﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;

public partial class Admin_taoTK : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //if (Session["MaUS"].ToString().Length == 0)
            //    Response.Redirect("login.aspx");
            //if (Session["Quyen"].ToString() == "1")
            //    Response.Redirect("xemthongbao.aspx");
            danhsach(0);

        }

    }
    protected void danhsach(int index)
    {
        clsTableAction clsTable = new clsTableAction("QuanTri");
        clsTable.exportDataToGridview("*", "", ref GridView1, 5, index);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        danhsach(e.NewPageIndex);
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        danhsach(GridView1.PageIndex);
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        danhsach(GridView1.PageIndex);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        clsTableAction clsTable = new clsTableAction("QuanTri");
        string id = GridView1.Rows[GridView1.EditIndex].Cells[0].Text;
        TextBox txt = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[1].Controls[0];
        string lableVi = txt.Text;
        TextBox txt2 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[2].Controls[0];
        string lableVi2 = txt2.Text;
        TextBox txt3 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[3].Controls[0];
        string lableVi3 = txt3.Text;
        TextBox txt4 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[4].Controls[0];
        string lableVi4 = txt4.Text;

        //Tien hanh update
        string sql = string.Format("update QuanTri set MaNV='{0}', HoTen='{1}', MatKhau ='{2}', Quyen = '{3}' where MaUS='{4}'", txt.Text, txt2.Text, txt3.Text, "3", id);



        if (clsTable.runSQL(sql))
        {
            GridView1.EditIndex = -1;
            danhsach(0);
        }
        else
        {
            tbl.Text = "that bai";
        }
        clsTable.free();
    }
    protected void butAdd_Click(object sender, EventArgs e)
    {
        if (txtTaikhoan.Text.Length == 0)
        {
            txtTaikhoan.BackColor = Color.Red;
            txtTaikhoan.Focus();
        }
        else
        {
            clsTableAction clsTable = new clsTableAction("Quantri");
            string sql = string.Format("insert into quantri values('{0}','{1}','{2}','{3}','{4}')", txtTaikhoan.Text, txtTaikhoan1.Text, txtTaikhoan2.Text, txtTaikhoan3.Text, "2");
            if (clsTable.runSQL(sql))
            {
                txtTaikhoan.BackColor = Color.White;
                txtTaikhoan.Focus();
                txtTaikhoan.Text = "";
                txtTaikhoan1.BackColor = Color.White;
                txtTaikhoan1.Focus();
                txtTaikhoan1.Text = "";
                txtTaikhoan2.BackColor = Color.White;
                txtTaikhoan2.Focus();
                txtTaikhoan2.Text = "";
                txtTaikhoan3.BackColor = Color.White;
                txtTaikhoan3.Focus();
                txtTaikhoan3.Text = "";
                danhsach(GridView1.PageIndex);
                add.Text = "đã thêm thành công";
            }
            else
            {
                txtTaikhoan.BackColor = Color.Black;
                add.Text = "that bai roi";
            }
            clsTable.free();
        }
    }
    protected void butXoa_Click(object sender, EventArgs e)
    {
        clsQueryList clsQuery = new clsQueryList();
        //clsTableAction clsTable = new clsTableAction("ThongBao");
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[5].FindControl("chkXoa");
            if (chk.Checked)
            {
                string id = GridView1.Rows[i].Cells[0].Text;
                //Response.Write(id);
                //Tien hanh xoa
                string sql = "delete from QuanTri where MaUS='" + id + "'";
                //string sql = string.Format("delete from thongbao where MaTB=''", id);
                clsQuery.addSQL(sql);
            }
        }
        if (clsQuery.Execute())
        {
            danhsach(GridView1.PageIndex);
            tbl.Text = ("ban da xoa thanh cong");
        }
        else
            tbl.Text = ("xoa that bai");
    }
}